/*
 * @Author: guoyangf
 * @Date: 2020-06-17 14:43:13
 * @LastEditTime: 2020-09-18 16:40:18
 * @Description: file content
 */
import { DongbaToLocalTime, getMultiLang, base, high } from 'nc-lightapp-front'
import moment from 'moment';
import Showtipss from '../events/Showtipss';

import dataFormat from 'src/erm/sscrppublic/common/utils/dataFormat';

import dtj from '../../../../../image/workBenchDetailLogos/-1.png';
import bh from '../../../../../image/workBenchDetailLogos/0.png';
import tg from '../../../../../image/workBenchDetailLogos/1.png';
import spz from '../../../../../image/workBenchDetailLogos/2.png';

import exceedImg from '../../../../../image/exceed.png'
import preImg from '../../../../../image/pre.png'

//卡片字段数据处理
function tableCardDataHandle(multiLang, taskcard, tasklist) {
    let listData = tasklist.rows;
    listData && taskcard.rows.map((one, index) => {
        //从列表上复制saga事务信息到detail
        const { values } = listData[index];
        one.values["saga_frozen"] = values.saga_frozen;
        one.values["saga_btxid"] = values.saga_btxid;
        one.values["saga_gtxid"] = values.saga_gtxid;
        one.values["saga_status"] = values.saga_status;
        one.values["pk_currenttask"] = values.pk_currenttask;
        one.values["pk_task"] = values.pk_task;
        one.values["ts"] = values.ts;
        one.values["module"] = values.module;
        one.values["taskstatus"] = values.taskstatus;
        one.values["iscmpreject"] = values.iscmpreject;

        let { billno, billdate, amount, approvestatus } = one.values;

        //billno && (billno.fieldType = '1'); //可绑定事件， 直接根据事件判断。

        //单据日期处理
        if (billdate) {
            let value = null;
            let field = null;
            if (billdate.display) {
                value = billdate.display;
                field = 'display';
            } else if (billdate.value) {
                value = billdate.value;
                field = 'value';
            }
            if (value) {
                let { _i } = DongbaToLocalTime(moment(value))
                if (_i) {
                    billdate[field] = dataFormat(getLocalTime(_i), 'date');
                } else {
                    billdate[field] = dataFormat(value, 'date');
                }

            }
        }

        //金额转换
        if (amount) {
            let value = amount.value;
            if (value) {
                amount.value = dataFormat(value, 'number');
            }
        }

        //审批状态处理
        approvestatus && getLogo.call(this, approvestatus);
    })
    return taskcard.rows;
}

function getLocalTime(_i) {
    let month = _i.getMonth() + 1
    if (month < 10) month = `0${month}`
    let date = _i.getDate()
    if (date < 10) date = `0${date}`
    return `${_i.getFullYear()}-${month}-${date}`
}

//根据审批状态生成logo配置
async function getLogo(obj) {
    let multiLang = await this.getMultiLangPromise;
    let status = obj.value;
    switch (status) {
        case '-1':
            obj.bgUrl = dtj;
            obj.txt = multiLang['job-approve-image-dtj']; /*待提交 */
            obj.className = 'logo-dtj';
            break;
        case '0':
            obj.bgUrl = bh;
            obj.txt = multiLang['job-approve-image-bh']; /*驳回*/
            obj.className = 'logo-bh';
            break;
        case '1':
            obj.bgUrl = tg;
            obj.txt = multiLang['job-approve-image-sptg']; /*审批通过*/
            obj.className = 'logo-sptg';
            break;
        case '2':
            obj.bgUrl = spz;
            obj.txt = multiLang['job-approve-image-spz'];  /*审批中*/
            obj.className = 'logo-spz';
            break;
    }
    obj.className = 'detail-logos ' + obj.className;
}

//列表字段自定义渲染
function tableFieldRender() {
    return {
        'billno': (text, record, index) => {
            let billnoJSX = (
                <a
                    title={record.billno.value}
                    className="billno"
                    style={{ cursor: 'pointer', marginRight: '5px' }}
                    onClick={
                        () => {
                            this.openBill(record, index)
                        }
                    }>
                    {record.billno.value}
                </a>
            )


            //打印不显示tips
            if (this.props.sscAppType == 'sscfile') {
                return billnoJSX;
            }

            return (
                <Showtipss
                    sscAppType={this.props.sscAppType}
                    isDoneTab={this.state.activeTabKey == 'done'} /* 已完成只显示出纳驳回 */
                    info={{ record, billnoJSX, multiLang: this.state.multiLang }}
                    setTipsWrapper={showtips => this.setState({ tipsInfo: [...showtips] })}
                />
            )

        },
        exceedstatus: (text, record, index) => {
            const { exceedstatus } = record
            let txt = '', src = ''
            if (exceedstatus) {  // exceed:超期状态
                /* exceedstatus.display = '{"hour": 12, "minute": "44"}'
                 exceedstatus.value = 'pre' */
                if (exceedstatus.value === 'exceed') {
                    txt = `${JSON.parse(exceedstatus.display).hour}${this.state.multiLang['job-approve-xs']}${JSON.parse(exceedstatus.display).minute}${this.state.multiLang['job-approve-fz']}`
                    src = exceedImg
                } else if (exceedstatus.value === 'pre') { // pre:即将超期 
                    txt = `${JSON.parse(exceedstatus.display).hour}${this.state.multiLang['job-approve-xs']}${JSON.parse(exceedstatus.display).minute}${this.state.multiLang['job-approve-fz']}`
                    src = preImg
                }
            }

            /* this.props.table.saveFilterCustomColData(this.state.activeTabKey, {
                attrcode: 'exceedstatus',
                rowId: this.props.table.getAllTableData(this.state.activeTabKey).rows[index].rowId,
                value: txt
            }) */
            return src && txt
                ? ([
                    <img style={{ width: '12px', marginRight: '4px', position: 'relative', top: '2px' }} src={src} />,
                    <span>{txt}</span>
                ])
                : null
        }
    }
}

function cardFieldRender() {
    return {
        'billno': (text, record, index) => {
            let billnoJSX = (
                <a
                    title={record.billno.value}
                    className="billno"
                    style={{ cursor: 'pointer', marginRight: '5px' }}
                    onClick={
                        () => {
                            this.openBill(record, index)
                        }
                    }>
                    {record.billno.value}
                </a>
            )
            return billnoJSX;
        }
    }
}

/**
 * 表格扩展列
 */
function tableExtendCol(multiLang) {
    let optCol = {
        label: multiLang['job-common-cz'],//操作
        itemtype: 'customer',
        attrcode: 'opr',
        visible: true,
        width: '205px',
        fixed: 'right',
        className: "opr"
    }
    return optCol;
}

//操作列自定义可见按钮。 默认操作列根据模板显示。特殊情况需要自定义可见性
function listOprColVisibleBtn() {
    return {
        //key为列表模板区域代码
        done: (record, areaVisibleBtns) => {
            let btnsVisibleObj = {}
            let visibleBtns = [];

            if (this.props.sscAppType == 'sscaudit' && record.def5) { //只有稽核单子才有整改状态
                let rectifyStatus = record.def5.value; //整改状态
                if (rectifyStatus != 'RECTIFIED') { // 因为有的单据不需要整改（NORECTIFIED），所以要加此处判断。
                    btnsVisibleObj['CancelRectify'] = false; //不是已整改时，不显示 取消整改 按钮。
                }
            }

            areaVisibleBtns.forEach((btn) => {
                let key = btn.key || btn;
                if (btnsVisibleObj[key] !== false) {
                    visibleBtns.push(btn)
                }
            })
            return visibleBtns;
        }
    }
}


export default tableCardDataHandle;
export { tableCardDataHandle, tableFieldRender, cardFieldRender, tableExtendCol, listOprColVisibleBtn }